package presentation.util;

import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * taken from 
 * http://jsfatwork.irian.at/semistatic/jsf.html#!idx:/jsf.html:fig:value-event-method-reg
 */

public class ParameterPhaseListener implements PhaseListener{

	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(ParameterPhaseListener.class);

	public void afterPhase(PhaseEvent pe) {}
	
	public void beforePhase(PhaseEvent arg0) {
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String, String> map = fc.getExternalContext().getRequestParameterMap();
		
		for (String key: map.keySet()){
			StringBuilder params = new StringBuilder();
			params.append("Parameter: ")
				.append(key.equals("subscriptionDetailsForm:supplier") || key.equals("subscriptionDetailsForm:purchaser") || key.equals("javax.faces.ViewState") ? "" : key)
				.append(key.equals("subscriptionDetailsForm:supplier") || key.equals("subscriptionDetailsForm:purchaser") || key.equals("javax.faces.ViewState") ? "" : " = ")
				.append( key.equals("subscriptionDetailsForm:supplier") || key.equals("subscriptionDetailsForm:purchaser") || key.equals("javax.faces.ViewState") ? "" : map.get(key));
			logger.debug(params.toString());			
		}
	}
	
	public PhaseId getPhaseId() {
		return PhaseId.APPLY_REQUEST_VALUES;
	}
}